package com.zone.vehicle.superApps.view.login.setSecret;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.zone.sdk.module.account.entity.CaptchaGetEntity;
import com.zone.sdk.module.account.entity.CheckCaptchaEntity;
import com.zone.vehicle.lib.view.captcha.model.CaptchaGetIt;
import com.zone.vehicle.lib.view.captcha.widget.BlockPuzzleDialog;
import com.zone.vehicle.lib.view.captcha.widget.DragImageView;
import com.zone.vehicle.superApps.databinding.FragmentResetSecretBinding;
import com.zone.vehicle.superApps.model.LoginViewModel;
import com.zone.vehicle.superApps.model.ViewModelFactory;
import com.zone.vehicle.superApps.view.common.HandledFragment;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

public class ResetSecretFragment  extends HandledFragment {

    private FragmentResetSecretBinding binding;
    private BlockPuzzleDialog blockPuzzleDialog;
    private LoginViewModel viewModel;

    public ResetSecretFragment(){

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
//        return inflater.inflate(R.layout.fragment_reset_secret, container, false);
        binding = FragmentResetSecretBinding.inflate(inflater, container, false);
        viewModel = new ViewModelProvider(this, new ViewModelFactory()).get(LoginViewModel.class);
        init();
        return binding.getRoot();
    }

    private void init(){
        blockPuzzleDialog = new BlockPuzzleDialog(this.requireContext());

        binding.btnFinish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewModel.captchaGet();
            }
        });

        blockPuzzleDialog.setDragListenner(new DragImageView.DragListenner() {
            @Override
            public void onDrag(double position) {
                CaptchaGetEntity entity = viewModel.getCaptchaGet().getValue();
                if (ObjectUtils.isNotEmpty(entity)){
                    viewModel.checkCaptcha(position, entity.getToken(), entity.getSecretKey());
                }
            }
        });

        viewModel.getCaptchaGet().observe(getViewLifecycleOwner(), new Observer<CaptchaGetEntity>() {
            @Override
            public void onChanged(CaptchaGetEntity captchaGetEntity) {
                if (ObjectUtils.isNotEmpty(captchaGetEntity)){
                    binding.loading.setVisibility(View.GONE);
                    CaptchaGetIt data = new CaptchaGetIt();
                    data.setJigsawImageBase64(captchaGetEntity.getJigsawImageBase64());
                    data.setOriginalImageBase64(captchaGetEntity.getOriginalImageBase64());
                    data.setSecretKey(captchaGetEntity.getSecretKey());
                    data.setToken(captchaGetEntity.getToken());
//                BeanUtils.copyProperties(captchaGetEntity, data);
                    blockPuzzleDialog.setData(data);
                    blockPuzzleDialog.show();
                }
            }
        });
        viewModel.getCheckCaptcha().observe(getViewLifecycleOwner(), new Observer<CheckCaptchaEntity>() {
            @Override
            public void onChanged(CheckCaptchaEntity checkCaptchaEntity) {
                if (ObjectUtils.isEmpty(checkCaptchaEntity)){
                    viewModel.captchaGet();
                }
            }
        });
        viewModel.getCaptcha_certificate().observe(getViewLifecycleOwner(), new Observer<String>() {
            @Override
            public void onChanged(String s) {
                if (StringUtils.isNotEmpty(s)){
                    blockPuzzleDialog.ok();
                    blockPuzzleDialog.cancel();
                }else {
                    viewModel.captchaGet();
                }
            }
        });

    }
}